﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Revis.VClinicNet.UI.Internal.Mvc.Infrastructure;

namespace Revis.VClinicNet.UI.Internal.Mvc.Extensions
{
    /// <summary>
    /// HtmlHelper extensions
    /// </summary>
    public static class HtmlHelperExtensions
    {
        public static MvcHtmlString TimeBox<T>(
            this HtmlHelper<T> html,
            string name,
            int value)
        {
            return html.TextBox(
                name,
                value,
                new { @class = "timebox" });
        }

        /// <summary>
        /// Show validation if required
        /// </summary>
        /// <param name="html">helerp object</param>
        /// <param name="message">Used message</param>
        /// <param name="className">css class for errors</param>
        /// <returns></returns>
        public static MvcHtmlString IfValidationFail(
            this HtmlHelper html,
            string message = "Przed kontynuowaniem popraw zidentyfikowane błędy",
            string className = "validation-error-main-message")
        {
            string result = String.Empty;
            if (html.ViewData.ModelState.IsValid == false) {
                result = String.Format("<span class=\"{0}\">{1}</span>", className, message);
            }
            MvcHtmlString formError = html.ValidationMessage(ModelStateDictionaryExtensions.ErrorMessageKey);
            result += String.Format(
                "<span class=\"{0}\">{1}</span>",
                "form-validation-error-message",
                formError != null ? formError.ToHtmlString() : String.Empty);
            return MvcHtmlString.Create(result);
        }

        /// <summary>
        /// List of logged user menu
        /// </summary>
        /// <param name="html">helper object</param>
        /// <returns></returns>
        public static IEnumerable<IMenuItem> UserMenuItems(this HtmlHelper html)
        {
            UserMenuViewModel model = html.ViewData.GetInstance<UserMenuViewModel>() ?? new UserMenuViewModel();
            return model.MenuItems;
        }

        /// <summary>
        /// Creates string for menu item
        /// </summary>
        /// <param name="html">helper object</param>
        /// <param name="item">menu item</param>
        /// <returns></returns>
        public static MvcHtmlString MenuItemLink(this HtmlHelper html, IMenuItem item)
        {
            return MvcHtmlString.Create(
                String.Format(
                    "<a href=\"{0}\">{1}</a>",
                    item.Url,
                    item.Value));
        }

        /// <summary>
        /// Create string with login controle
        /// </summary>
        /// <param name="html">helper object</param>
        /// <returns></returns>
        public static MvcHtmlString DrawLoginControle(
            this HtmlHelper html)
        {
            string userName = html.LoggedUserName();
            string result = String.Empty;
            if (String.IsNullOrEmpty(userName) == false) {
                result = String.Format("Zalogowany jako {0} [{1}]",
                    userName, html.ActionLink("Wyloguj", "Logout", "Login").ToString());
            } else {
                result = html.ActionLink("Zaloguj", "Index", "Login").ToString();
            }
            return MvcHtmlString.Create(result);
        }

        /// <summary>
        /// Returns logged user name
        /// </summary>
        /// <param name="html"></param>
        /// <returns></returns>
        public static string LoggedUserName(this HtmlHelper html)
        {
            ClinicUserViewModel user = html.ViewData.GetInstance<ClinicUserViewModel>();
            return user != null ? user.UserName : String.Empty;
        }

        /// <summary>
        /// Return string with back link if required
        /// </summary>
        /// <param name="html"></param>
        /// <param name="text">link text</param>
        /// <returns></returns>
        public static MvcHtmlString IfBackLinkRequired(
            this HtmlHelper html,
            string text = "Wróć")
        {
            IBackLink backLink = html.ViewContext.HttpContext.Session.GetInstance<IBackLink>();
            string result = String.Empty;
            if (backLink != null) {
                result = String.Format("<a href=\"{0}\">{1}</a>", backLink.Url, text);
            }
            return MvcHtmlString.Create(result);
        }

        /// <summary>
        /// Return string with yes submit button
        /// </summary>
        /// <param name="html"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static MvcHtmlString YesButton(
            this HtmlHelper html,
            string value = "Tak")
        {
            string result = String.Format("<input type=\"submit\" value=\"{0}\" />", value);
            return MvcHtmlString.Create(result);
        }

        /// <summary>
        /// return string with no submit button
        /// </summary>
        /// <param name="html"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static MvcHtmlString NoButton(
            this HtmlHelper html,
            string value = "Nie")
        {
            string result = String.Format(
                "<input type=\"button\" value=\"{0}\" onclick=\"history.back()\"/>", value);
            return MvcHtmlString.Create(result);
        }
    }
}